/*
 * Copyright (c) 2025 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:quill_native_bridge_platform_interface/internal.dart';
import 'package:quill_native_bridge_platform_interface/quill_native_bridge_platform_interface.dart';
import 'src/messages.g.dart';
/// An implementation of [QuillNativeBridgePlatform] for ohos.
class QuillNativeBridgeOhos extends QuillNativeBridgePlatform{
  final QuillNativeBridgeApi _hostApi = QuillNativeBridgeApi();

  /// Registers this class as the default instance of [QuillNativeBridgePlatform].
  static void registerWith() {
    QuillNativeBridgePlatform.instance = QuillNativeBridgeOhos();
  }

  @override
  Future<bool> isSupported(QuillNativeBridgeFeature feature) async => {
    QuillNativeBridgeFeature.getClipboardHtml,
    QuillNativeBridgeFeature.copyHtmlToClipboard,
    QuillNativeBridgeFeature.copyImageToClipboard,
    QuillNativeBridgeFeature.getClipboardImage,
    QuillNativeBridgeFeature.getClipboardGif,
    QuillNativeBridgeFeature.openGalleryApp,
    QuillNativeBridgeFeature.saveImageToGallery,
  }.contains(feature);

  @override
  Future<String?> getClipboardHtml() async => _hostApi.getClipboardHtml();

  @override
  Future<void> copyHtmlToClipboard(String html) =>
      _hostApi.copyHtmlToClipboard(html);

  @override
  Future<Uint8List?> getClipboardImage() async {
    try {
      return await _hostApi.getClipboardImage();
    } on PlatformException catch (e) {
      if (kDebugMode &&
          (e.code == 'FILE_READ_PERMISSION_DENIED' ||
              e.code == 'FILE_NOT_FOUND')) {
        _printOhosClipboardImageAccessKnownIssue(e);
        return null;
      }
      rethrow;
    }
  }

  @override
  Future<void> copyImageToClipboard(Uint8List imageBytes) async {
    try {
      await _hostApi.copyImageToClipboard(imageBytes);
    } on PlatformException catch (e) {
      assert(() {
        if (e.code == 'OHOS_MANIFEST_NOT_CONFIGURED') {
          throw StateError(
            '\n ${e.toString()}',
          );
        }
        return true;
      }());

      rethrow;
    }
  }

  @override
  Future<Uint8List?> getClipboardGif() async {
    try {
      return await _hostApi.getClipboardGif();
    } on PlatformException catch (e) {
      if (kDebugMode &&
          (e.code == 'FILE_READ_PERMISSION_DENIED' ||
              e.code == 'FILE_NOT_FOUND')) {
        _printOhosClipboardImageAccessKnownIssue(e);
        return null;
      }
      rethrow;
    }
  }

  void _printOhosClipboardImageAccessKnownIssue(PlatformException e) {
    if (kDebugMode) {
      debugPrint('Platform error details: ${e.toString()}',);
    }
  }

  @override
  Future<void> openGalleryApp() => _hostApi.openGalleryApp();

  @override
  Future<void> saveImageToGallery(
      Uint8List imageBytes, {
        required GalleryImageSaveOptions options,
      }) async {
    try {
      await _hostApi.saveImageToGallery(
        imageBytes,
        name: options.name,
        fileExtension: options.fileExtension,
        mimeType: getImageMimeType(options.fileExtension),
        albumName: options.albumName,
      );
    } on PlatformException catch (e) {
      assert(() {
        if (e.code == 'OHOS_MANIFEST_NOT_CONFIGURED') {
          throw StateError('\n${e.toString()}',
          );
        }
        return true;
      }());
      rethrow;
    }
  }
}
